package com.letv.android.client.utils;

import java.util.Timer;
import java.util.TimerTask;

import com.letv.component.player.videoview.VideoViewH264m3u8;
import com.letv.core.utils.LogInfo;

/**
 * 该类用于处理卡顿逻辑
 * 
 * @author zhuqiao
 *
 */
public class LetvBlockLogic {
    private OnBlockCallBack mCallBack;
    /**
     * 最近一次卡顿出现的时间
     */
    private long lastBlockTime = -1;
    /**
     * 开始loading的时间
     */
    private long beginLoading = -1;
    /**
     * 当前是否处于loading状态
     */
    public boolean isLoading = false;
    /**
     * 是否启动了timer每5秒进行判断
     */
    private boolean start = false;

    private boolean isExit = false;

    public LetvBlockLogic(OnBlockCallBack callBack) {
        mCallBack = callBack;
    }

    /**
     * 
     * @param loading 当前是否处于loading状态
     */
    public boolean isOpen;

    public void onLoading(boolean loading, int mCurrentState) {
        if (mCurrentState == VideoViewH264m3u8.STATE_PREPARED) {
            beginLoading = -1;
            lastBlockTime = -1;
        } else if (mCurrentState == VideoViewH264m3u8.STATE_PAUSED
                || mCurrentState == VideoViewH264m3u8.STATE_ENFORCEMENT) {
            isOpen = true;
        }
        if (mCurrentState == VideoViewH264m3u8.STATE_STOPBACK
                || mCurrentState == VideoViewH264m3u8.STATE_PLAYBACK_COMPLETED) {
            exitBlockLogic();
            beginLoading = -1;
            lastBlockTime = -1;
            isOpen = false;
            isLoading = false;
            return;
        }
        if (mCurrentState == 0) {
            return;
        }
        if (!isOpen) {
            return;
        }
        long currentTime = System.currentTimeMillis();
        if (loading) {
            LogInfo.log("zhuqiao", "5s卡顿>>>>>>>>>>>>>>>>>>>>>>>>");
            startTimer();
            /**
             * 因为存在处于loading状态多次调用的问题，为了防止出错，如果当前已经处于onloading状态则不再刷新beginLoading的时间点
             */
            if (!isLoading) {
                beginLoading = currentTime;
                LogInfo.log("zhuqiao", "beginLoading " + beginLoading);
            }
        } else {
            LogInfo.log("zhuqiao", "2s卡顿>>>>>>>>>>>>>>>>>>>>>>>>");
            /**
             * 超过2S才算卡顿
             */
            LogInfo.log("zhuqiao", "超过时间" + currentTime + ";" + beginLoading + ";" + (currentTime - beginLoading));
            if (currentTime - beginLoading > 2000 && currentTime - beginLoading < 5000) {

                /**
                 * 两次卡顿时间间隔小于30S
                 */
                LogInfo.log("zhuqiao", " 两次卡顿时间间隔小于30S =" + (currentTime - lastBlockTime));
                if (currentTime - lastBlockTime < 30 * 1000) {
                    if (mCallBack != null) {
                        LogInfo.log("zhuqiao", "currentTime - lastBlockTime < 30 * 1000");
                        mCallBack.commonBlock();
                    }
                }
                lastBlockTime = currentTime;
            }
            beginLoading = -1;
            hasCallBack = false;
        }
        isLoading = loading;
    }

    /**
     * 5S卡顿是否已经回调
     */
    private boolean hasCallBack = false;

    /**
     * 启动定时器，每隔半秒判断当前卡顿是否超过5S
     */
    private void startTimer() {
        if (!start) {
            LogInfo.log("zhuqiao", "5s开始计时>>>>>>>>>>>>>>>>>>>>>>>> ");
            synchronized (LetvBlockLogic.this) {
                if (!start) {
                    final Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            LogInfo.log("zhuqiao", "TimerTaskStart");
                            if (isExit) {
                                timer.cancel();
                            }
                            LogInfo.log("zhuqiao", "isLoading = " + isLoading + "beginLoading = " + beginLoading
                                    + "----System.currentTimeMillis() - beginLoading"
                                    + (System.currentTimeMillis() - beginLoading));
                            if (isLoading && beginLoading != -1 && (System.currentTimeMillis() - beginLoading >= 5000)) {
                                if (!hasCallBack) {
                                    if (mCallBack != null) {
                                        LogInfo.log("zhuqiao", "5s回调>>>>>>>>>>>>>>>>>>>>>>>>");
                                        mCallBack.blockFiveSeconds();
                                    }
                                    hasCallBack = true;
                                }
                            }
                            timer.cancel();
                            start = false;
                        }
                    }, 5000);
                    if (isExit) {
                        timer.cancel();
                    }
                    start = true;
                }
            }
        }
    }

    public void exitBlockLogic() {
        isExit = true;
    }

    public interface OnBlockCallBack {
        /**
         * 当30S内出现两次超过2S的卡顿，调用此方法
         */
        void commonBlock();

        /**
         * 单次卡顿超过5S调用此方法
         */
        void blockFiveSeconds();

        /**
         * 用于传递提示信息
         * 
         * @param msg
         */
        void blockSuggest(String msg);
    }
}
